package com.firstgroup.dms.service.impl;

import com.firstgroup.dms.common.Result;
import com.firstgroup.dms.pojo.User;
import com.firstgroup.dms.mapper.UserMapper;
import com.firstgroup.dms.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        return userMapper.login(username, password);
    }

    @Override
    public Result<Object> changePwd(String username, String oldPassword, String newPassword) {
        System.out.println("Service层 - 用户名：" + username);
        System.out.println("Service层 - 原密码：" + oldPassword);

        // 先查询用户信息
        User dbUser = userMapper.getUserByName(username);
        if (dbUser == null) {
            return Result.error("用户不存在");
        }

        System.out.println("数据库中的密码：" + dbUser.getUserPwd());

        // 验证原密码
        if (!dbUser.getUserPwd().equals(oldPassword)) {
            return Result.error("原密码错误");
        }

        // 更新密码
        int rows = userMapper.updatePwd(dbUser.getUserId(), newPassword);
        if (rows > 0) {
            return Result.success();
        } else {
            return Result.error("修改密码失败");
        }
    }
}
